# -*- coding: utf-8 -*-
import pandas as pd
# from .model.expert_strategy import MACD, MACD_rt
from .model.expert_strategy import *
from .model.unionmodel import UNION_Strategy
import inspect
from .model import expert_strategy

class SignGeneration(object):
    def __init__(self, model=None):
        self.buy_sign = False
        self.sell_sign = False
        self.model = model
        self.feature_df = pd.DataFrame()
        self.raw_data = pd.DataFrame()

        self.expert_strategy_list = []
        self.strategy_name = []
        for name, obj in inspect.getmembers(expert_strategy, inspect.isclass):
            if inspect.isclass(obj):
                self.expert_strategy_list.append(obj)
                self.strategy_name.append(name)

    def data_update(self, code, current_data):
        """

        """
        if isinstance(current_data, pd.Series) or isinstance(current_data, pd.DataFrame):
            self.raw_data = self.raw_data._append(current_data, ignore_index=True)
        else:
            raise TypeError("current_data must be a pandas.Series")
        code_df = self.raw_data[self.raw_data["code"] == code]
        return code_df

    def get_sign(self):
        return self.buy_sign, self.sell_sign

    def clean_history(self):
        self.raw_data = pd.DataFrame()

    def generate_sign_rt(self, code, day_df, strategy_name="MACD", union_weight=None):
        self.data_update(code, day_df)
        # 如果数据不足 15条，不做操作
        if self.raw_data.shape[0] < 15:
            return False, False
        else:
            if self.model is None:
                if strategy_name == "UNION":
                    union = UNION_Strategy(weight=union_weight)
                    self.feature_df = union.update(self.raw_data)
                elif strategy_name in self.strategy_name:
                    # 匹配策略
                    for indicate in range(len(self.strategy_name)):
                        # print(strategy_name, self.strategy_name[indicate], strategy_name == self.strategy_name[indicate])
                        if strategy_name == self.strategy_name[indicate]:
                            strategy = self.expert_strategy_list[indicate]()
                            self.feature_df = strategy.update(self.raw_data)
                            # print(self.feature_df)

                else:
                    raise ValueError("strategy_name must be [KDJ, MACD]")
            else:
                raise NotImplementedError("model is not implemented")

            return self.feature_df